package com.killer_jin.core.common.util;

import org.hibernate.validator.internal.util.StringHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jiayu
 * @since 2019/07/08
 */
public class VerificationUtils {
    public static final String REGEX_NUMERIC = "[0-9]";
    public static final String REGEX_VIN = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{16,18}";
    public static final String REGEX_PHONE = "[1][0-9]{10}";
    public static final String REGEX_CAR_OWNER_PHONE_NUMBER = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";
    public static final String REGEX_VEHICLE_NUMBER = "(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]([A-HJ-Z][A-HJ-NP-Z0-9]{5})$)";
    private static Pattern numericPattern = Pattern.compile(REGEX_NUMERIC);
    private static Pattern vinPattern = Pattern.compile(REGEX_VIN);
    private static Pattern phoneNumber = Pattern.compile(REGEX_PHONE);
    private static Pattern carOwnerPhoneNumber = Pattern.compile(REGEX_CAR_OWNER_PHONE_NUMBER);
    private static Pattern numberPattern = Pattern.compile(REGEX_VEHICLE_NUMBER);

    /**
     * 利用正则表达式判断字符串是否是数字
     *
     * @param str str
     * @return 結果
     */
    public static boolean isNumeric(String str) {
        if (StringHelper.isNullOrEmptyString(str)) {
            return false;
        }
        Matcher matcher = numericPattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 7-8 车牌号校验
     *
     * @param number
     * @return 結果
     */
    public static boolean isVehicleNumber(String number) {
        if (number != null && (number.length() == 8 || number.length() == 7)) {
            Matcher matcher = numberPattern.matcher(number);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 16-18位 英文数字  车架号校验
     *
     * @param vin vin
     * @return 結果
     */
    public static boolean isVin(String vin) {
        if (StringHelper.isNullOrEmptyString(vin)) {
            return false;
        }
        Matcher matcher = vinPattern.matcher(vin);
        return matcher.matches();
    }

    /**
     * 利用正则表达式判断11位数字手机号,必须1开头
     *
     * @param str str
     * @return 結果
     */
    public static boolean isPhoneNumber(String str) {
        Matcher matcher = phoneNumber.matcher(str);
        return matcher.matches();
    }

    /**
     * 利用正则表达式判断11位数字手机号
     *
     * @param str str
     * @return 結果
     */
    public static boolean isCarOwnerPhoneNumber(String str) {
        Matcher matcher = carOwnerPhoneNumber.matcher(str);
        return matcher.matches();
    }
}
